/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;

namespace Xceed.Silverlight.ListBox.Themes
{
  public abstract class MetroThemeResourceDictionaryBase : ThemeResourceDictionary
  {
    #region Static Members

    private const string MetroAccentColorBrushKey = "MetroAccentColorBrush";

    private static Color TealAccent = Color.FromArgb( 0xFF, 0x00, 0xAB, 0xA9 );
    private static Color RedAccent = Color.FromArgb( 0xFF, 0xE5, 0x14, 0x00 );
    private static Color PurpleAccent = Color.FromArgb( 0xFF, 0xA2, 0x00, 0xFF );
    private static Color PinkAccent = Color.FromArgb( 0xFF, 0xE6, 0x71, 0xB8 );
    private static Color OrangeAccent = Color.FromArgb( 0xFF, 0xF0, 0x96, 0x09 );
    private static Color MagentaAccent = Color.FromArgb( 0xFF, 0xFF, 0x00, 0x97 );
    private static Color LimeAccent = Color.FromArgb( 0xFF, 0x8C, 0xBF, 0x26 );
    private static Color GreenAccent = Color.FromArgb( 0xFF, 0x33, 0x99, 0x33 );
    private static Color BrownAccent = Color.FromArgb( 0xFF, 0x99, 0x66, 0x00 );
    private static Color BlueAccent = Color.FromArgb( 0xFF, 0x1B, 0xA1, 0xE2 );

    private static SolidColorBrush GetSolidColorBrushFromAccentColor( MetroAccentColor accentColor )
    {
      SolidColorBrush brush = null;

      switch( accentColor )
      {
        case MetroAccentColor.Teal:
        {
          brush = new SolidColorBrush( MetroThemeResourceDictionaryBase.TealAccent );
          break;
        }

        case MetroAccentColor.Red:
        {
          brush = new SolidColorBrush( MetroThemeResourceDictionaryBase.RedAccent );
          break;
        }

        case MetroAccentColor.Purple:
        {
          brush = new SolidColorBrush( MetroThemeResourceDictionaryBase.PurpleAccent );
          break;
        }

        case MetroAccentColor.Pink:
        {
          brush = new SolidColorBrush( MetroThemeResourceDictionaryBase.PinkAccent );
          break;
        }

        case MetroAccentColor.Orange:
        {
          brush = new SolidColorBrush( MetroThemeResourceDictionaryBase.OrangeAccent );
          break;
        }

        case MetroAccentColor.Magenta:
        {
          brush = new SolidColorBrush( MetroThemeResourceDictionaryBase.MagentaAccent );
          break;
        }

        case MetroAccentColor.Lime:
        {
          brush = new SolidColorBrush( MetroThemeResourceDictionaryBase.LimeAccent );
          break;
        }

        case MetroAccentColor.Green:
        {
          brush = new SolidColorBrush( MetroThemeResourceDictionaryBase.GreenAccent );
          break;
        }

        case MetroAccentColor.Brown:
        {
          brush = new SolidColorBrush( MetroThemeResourceDictionaryBase.BrownAccent );
          break;
        }

        case MetroAccentColor.Blue:
        {
          brush = new SolidColorBrush( MetroThemeResourceDictionaryBase.BlueAccent );
          break;
        }

        case MetroAccentColor.Custom:
        {
          brush = null;
          break;
        }

        default:
        {
          Debug.Assert( false, "MetroAccentColor not accounted for in the generation of the accent brush." );
          break;
        }
      }

      return brush;
    }

    #endregion

    protected MetroThemeResourceDictionaryBase()
      : base()
    {
      this.UpdateResources();
    }

    #region DefaultAccentColor Property

    protected virtual MetroAccentColor DefaultAccentColor
    {
      get
      {
        return MetroAccentColor.Blue;
      }
    }

    private MetroAccentColor GetDefaultAccentColor()
    {
      MetroAccentColor defaultAccentColor = this.DefaultAccentColor;

      if( defaultAccentColor == MetroAccentColor.Custom )
        throw new InvalidOperationException( "DefaultAccentColor cannot return MetroAccentColor.Custom." );

      return defaultAccentColor;
    }

    #endregion

    #region AccentColor Property

    public MetroAccentColor AccentColor
    {
      get
      {
        return m_accentColor;
      }
      set
      {
        if( m_accentColor != value )
        {
          m_accentColor = value;
          this.UpdateResources();
        }
      }
    }

    #endregion

    #region CustomAccentBrush

    public Brush CustomAccentBrush
    {
      get
      {
        return m_customAccentBrush;
      }
      set
      {
        m_customAccentBrush = value;

        if( m_accentColor == MetroAccentColor.Custom )
        {
          this.UpdateResources();
        }
      }
    }

    #endregion

    protected override void UpdateResources()
    {
      this.Clear();

      if( this.StyleUsageMode == Silverlight.ListBox.StyleUsageMode.Implicit )
      {
        MetroStylesBase implicitStyles = this.PrepareImplicitStyles();
        this.MergedDictionaries.Add( implicitStyles );
      }
      else
      {
        MetroStylesBase explicitStyles = this.PrepareExplicitStyles();
        this.MergedDictionaries.Add( explicitStyles );
      }
    }

    #region Resource Dictionary Builder

    internal abstract MetroStylesBase CreateImplicitDictionaryCore();
    internal abstract MetroStylesBase CreateExplicitDictionaryCore();

    private MetroStylesBase PrepareImplicitStyles()
    {
      MetroStylesBase implicitStyles = this.CreateImplicitDictionaryCore();

      MetroStylesBase explicitStyles = this.PrepareExplicitStyles();

      implicitStyles.MergedDictionaries.Add( explicitStyles );
      implicitStyles.Initialize();

      return implicitStyles;
    }

    private MetroStylesBase PrepareExplicitStyles()
    {
      MetroStylesBase explicitStyles = this.CreateExplicitDictionaryCore();

      MetroAccentColor accentColor = this.AccentColor;

      Brush accentBrush = null;

      if( accentColor == MetroAccentColor.Custom )
      {
        accentBrush = this.CustomAccentBrush;
      }
      else
      {
        accentBrush = MetroThemeResourceDictionaryBase.GetSolidColorBrushFromAccentColor( this.AccentColor );
      }

      if( accentBrush == null )
      {
        // Fallback to default brush.
        accentBrush = MetroThemeResourceDictionaryBase.GetSolidColorBrushFromAccentColor( this.GetDefaultAccentColor() );
      }

      explicitStyles.EnsureResource( MetroThemeResourceDictionaryBase.MetroAccentColorBrushKey, accentBrush );

      explicitStyles.Initialize();

      return explicitStyles;
    }

    #endregion

    #region Private Fields

    private MetroAccentColor m_accentColor;
    private Brush m_customAccentBrush;

    #endregion
  }
}
